package com.photo.photePro.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.photo.photePro.common.R;
import com.photo.photePro.entity.Comment;
import com.photo.photePro.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 评论Controller
 */
@Slf4j
@RestController
@RequestMapping("/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 添加评论
     */
    @PostMapping
    public R<String> save(@RequestBody Comment comment) {
        comment.setLikeCount(0);
        comment.setStatus(1); // 默认通过
        comment.setCreateTime(LocalDateTime.now());
        comment.setUpdateTime(LocalDateTime.now());
        
        commentService.save(comment);
        return R.success("评论成功");
    }

    /**
     * 分页查询评论
     */
    @GetMapping("/page")
    public R<Page<Comment>> page(int page, int pageSize, String content, Integer status) {
        log.info("分页查询评论，page={}, pageSize={}, content={}, status={}", page, pageSize, content, status);
        
        Page<Comment> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加内容查询条件
        if (StringUtils.hasText(content)) {
            queryWrapper.like(Comment::getContent, content);
        }
        
        // 添加状态查询条件
        if (status != null) {
            queryWrapper.eq(Comment::getStatus, status);
        }
        
        // 添加排序条件
        queryWrapper.orderByDesc(Comment::getCreateTime);
        
        // 执行查询
        commentService.page(pageInfo, queryWrapper);
        
        return R.success(pageInfo);
    }

    /**
     * 根据摄影作品ID查询评论列表
     */
    @GetMapping("/photo/{photoId}")
    public R<List<Comment>> listByPhotoId(@PathVariable Long photoId) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getPhotoId, photoId);
        queryWrapper.eq(Comment::getStatus, 1); // 已通过的评论
        queryWrapper.orderByDesc(Comment::getCreateTime);
        
        List<Comment> list = commentService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 分页查询用户评论
     */
    @GetMapping("/user")
    public R<Page<Comment>> pageByUserId(int page, int pageSize, Long userId) {
        log.info("分页查询用户评论，page={}, pageSize={}, userId={}", page, pageSize, userId);
        
        if (userId == null) {
            return R.error("用户ID不能为空");
        }
        
        Page<Comment> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getUserId, userId);
        queryWrapper.orderByDesc(Comment::getCreateTime);
        
        commentService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 根据用户ID查询评论列表
     */
    @GetMapping("/user/{userId}")
    public R<List<Comment>> listByUserId(@PathVariable Long userId) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getUserId, userId);
        queryWrapper.orderByDesc(Comment::getCreateTime);
        
        List<Comment> list = commentService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable Long id) {
        commentService.removeById(id);
        return R.success("删除成功");
    }

    /**
     * 修改评论内容
     */
    @PutMapping("/{id}")
    public R<String> update(@PathVariable Long id, @RequestBody Comment comment) {
        log.info("修改评论: id={}, content={}", id, comment.getContent());
        
        Comment existingComment = commentService.getById(id);
        if (existingComment == null) {
            return R.error("评论不存在");
        }
        
        // 只允许修改评论内容
        existingComment.setContent(comment.getContent());
        existingComment.setUpdateTime(LocalDateTime.now());
        commentService.updateById(existingComment);
        
        return R.success("修改评论成功");
    }

    /**
     * 管理员审核评论
     */
    @PutMapping("/status")
    public R<String> updateStatus(@RequestBody Comment comment) {
        Comment existingComment = commentService.getById(comment.getId());
        if (existingComment == null) {
            return R.error("评论不存在");
        }
        
        existingComment.setStatus(comment.getStatus());
        existingComment.setUpdateTime(LocalDateTime.now());
        commentService.updateById(existingComment);
        
        return R.success("审核成功");
    }
} 